﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin.Controls
{
    public partial class EditProfile : System.Web.UI.UserControl
    {
        public string user;
        public string email;
        public string fullname;
        public string phone;
        public string address;
        public string avatar;
        public string role;
        public int qlaccount;
        public int qltour;
        public int qlmenu;
        public int qlhoadon;
        public int qlthongtin;
        public int qlgiaodien;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["idaccount"] != null)
            {
                int id = Convert.ToInt32(Request.QueryString["idaccount"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereID("tbl_Account", "Account_ID", id.ToString());
                user = dt.Rows[0]["Account_User"].ToString();
                email = dt.Rows[0]["Account_Email"].ToString();
                fullname = dt.Rows[0]["Account_FullName"].ToString();
                phone = dt.Rows[0]["Account_Phonenumber"].ToString();
                address = dt.Rows[0]["Account_Address"].ToString();
                avatar = dt.Rows[0]["Account_Avatar"].ToString();
                role = dt.Rows[0]["Account_Role"].ToString();
                qlaccount = Convert.ToInt32(dt.Rows[0]["Account_QL1"].ToString());
                qltour = Convert.ToInt32(dt.Rows[0]["Account_QL2"].ToString());
                qlmenu = Convert.ToInt32(dt.Rows[0]["Account_QL3"].ToString());
                qlhoadon = Convert.ToInt32(dt.Rows[0]["Account_QL4"].ToString());
                qlthongtin = Convert.ToInt32(dt.Rows[0]["Account_QL5"].ToString());
                qlgiaodien = Convert.ToInt32(dt.Rows[0]["Account_QL6"].ToString());

                if (qlaccount == 1)
                {
                    cbxqlaccount.Checked = true;
                }
                if (qltour == 1)
                {
                    cbxqltour.Checked = true;
                }
                if (qlmenu == 1)
                {
                    cbxqlmenu.Checked = true;
                }
                if (qlhoadon == 1)
                {
                    cbxqlhoadon.Checked = true;
                }
                if (qlthongtin == 1)
                {
                    cbxqlthongtin.Checked = true;
                }
                if (qlgiaodien == 1)
                {
                    cbxqlgiaodien.Checked = true;
                }

                txtEmail.Text = email;
                txtFullname.Text = fullname;
                txtPhone.Text = phone;
                txtUsername.Text = user;
                ddlRole.Text = role;
                txtAddress.Text = address;

                ddlRole_SelectedIndexChanged(sender, e);

            }

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            int user_id = Convert.ToInt32(Request.QueryString["idaccount"]);
            if (cbxqlaccount.Checked == true)
            {
                qlaccount = 1;
            }
            else
            {
                qlaccount = 0;
            }


            if (cbxqlgiaodien.Checked == true)
            {
                qlgiaodien = 1;
            }
            else
            {
                qlgiaodien = 0;
            }


            if (cbxqlhoadon.Checked == true)
            {
                qlhoadon = 1;
            }
            else
            {
                qlhoadon = 0;
            }


            if (cbxqltour.Checked == true)
            {
                qltour = 1;
            }
            else
            {
                qltour = 0;
            }


            if (cbxqlmenu.Checked == true)
            {
                qlmenu = 1;
            }
            else
            {
                qlmenu = 0;
            }


            if (cbxqlthongtin.Checked == true)
            {
                qlthongtin = 1;
            }
            else
            {
                qlthongtin = 0;
            }
            string email = txtEmail.Text;
            string fullname = txtFullname.Text;
            string address = txtAddress.Text;
            string mobile = txtPhone.Text;
            string filePath = null;
            string username = txtUsername.Text;
            DataTable checkuseremail = DAL.DCollector.DBCollector.Instance.CallStored("callstored", "SELECT count (*) as result FROM (SELECT *   FROM tbl_Account where Account_ID <> " + user_id + ") tbl WHERE Account_User = '" + username + "' OR Account_Email = '" + email + "'", true);
            if (checkuseremail.Rows[0]["result"].ToString() == "0")
            {
                if (fulAvatar.HasFile)
                {
                    if (LIB.Validate.CheckFileType(fulAvatar.FileName))
                    {
                        string link = fulAvatar.FileName;
                        LIB.Validate saveimage = new LIB.Validate();
                        saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Images-Account/"), link, 250, fulAvatar.PostedFile.InputStream);
                        filePath = "/Resources/Images/Images-Account/" + fulAvatar.FileName;
                        if (avatar == "")
                        {
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[12];
                            cl[0] = new DAL.DHelpers.Column("Account_User", username, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[1] = new DAL.DHelpers.Column("Account_Email", email, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[2] = new DAL.DHelpers.Column("Account_FullName", fullname, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Account_Address", address, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[4] = new DAL.DHelpers.Column("Account_Phonenumber", mobile, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[5] = new DAL.DHelpers.Column("Account_Avatar", filePath, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[6] = new DAL.DHelpers.Column("Account_QL1", qlaccount.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[7] = new DAL.DHelpers.Column("Account_QL2", qltour.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[8] = new DAL.DHelpers.Column("Account_QL3", qlmenu.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[9] = new DAL.DHelpers.Column("Account_QL4", qlhoadon.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[10] = new DAL.DHelpers.Column("Account_QL5", qlthongtin.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[11] = new DAL.DHelpers.Column("Account_QL6", qlgiaodien.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);

                            string[] colWhere = new string[1];
                            colWhere[0] = "Account_ID";

                            object[] colValue = new object[1];
                            colValue[0] = user_id;

                            DAL.DCollector.DBCollector.Instance.Update("tbl_Account", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=0&sub=1");
                        }
                        else
                        {
                            File.Delete(MapPath(avatar));
                            DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[12];
                            cl[0] = new DAL.DHelpers.Column("Account_User", username, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[1] = new DAL.DHelpers.Column("Account_Email", email, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[2] = new DAL.DHelpers.Column("Account_FullName", fullname, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[3] = new DAL.DHelpers.Column("Account_Address", address, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                            cl[4] = new DAL.DHelpers.Column("Account_Phonenumber", mobile, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[5] = new DAL.DHelpers.Column("Account_Avatar", filePath, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                            cl[6] = new DAL.DHelpers.Column("Account_QL1", qlaccount.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[7] = new DAL.DHelpers.Column("Account_QL2", qltour.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[8] = new DAL.DHelpers.Column("Account_QL3", qlmenu.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[9] = new DAL.DHelpers.Column("Account_QL4", qlhoadon.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[10] = new DAL.DHelpers.Column("Account_QL5", qlthongtin.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                            cl[11] = new DAL.DHelpers.Column("Account_QL6", qlgiaodien.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);

                            string[] colWhere = new string[1];
                            colWhere[0] = "Account_ID";

                            object[] colValue = new object[1];
                            colValue[0] = user_id;

                            DAL.DCollector.DBCollector.Instance.Update("tbl_Account", cl, colWhere, colValue);
                            Response.Redirect("/Admin/Index.aspx?panel=0&sub=1");
                        }
                    }
                    else
                    {
                        lblCheckFileImages.Text = "Chỉ có thể upload file(.jpg,.png,.jpeg)";
                    }
                }
                else
                {
                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[11];
                    cl[0] = new DAL.DHelpers.Column("Account_User", username, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[1] = new DAL.DHelpers.Column("Account_Email", email, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[2] = new DAL.DHelpers.Column("Account_FullName", fullname, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[3] = new DAL.DHelpers.Column("Account_Address", address, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                    cl[4] = new DAL.DHelpers.Column("Account_Phonenumber", mobile, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    cl[5] = new DAL.DHelpers.Column("Account_QL1", qlaccount.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                    cl[6] = new DAL.DHelpers.Column("Account_QL2", qltour.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                    cl[7] = new DAL.DHelpers.Column("Account_QL3", qlmenu.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                    cl[8] = new DAL.DHelpers.Column("Account_QL4", qlhoadon.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                    cl[9] = new DAL.DHelpers.Column("Account_QL5", qlthongtin.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);
                    cl[10] = new DAL.DHelpers.Column("Account_QL6", qlgiaodien.ToString(), DAL.DHelpers.DataType.DataTypeSql.Int);

                    string[] colWhere = new string[1];
                    colWhere[0] = "Account_ID";

                    object[] colValue = new object[1];
                    colValue[0] = user_id;

                    DAL.DCollector.DBCollector.Instance.Update("tbl_Account", cl, colWhere, colValue);
                    Response.Redirect("/Admin/Index.aspx?panel=0&sub=1");
                }
            }
            else
            {
                Response.Write("<script>alert('Tài khoản hoặc Email này đã tồn tại...')</script>");
            }

        }

        protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlRole.Text.Equals("Mod"))
            {
                lblqlmenu.Visible = true;
                cbxqlaccount.Visible = true;
                cbxqlgiaodien.Visible = true;
                cbxqlhoadon.Visible = true;
                cbxqlmenu.Visible = true;
                cbxqlthongtin.Visible = true;
                cbxqltour.Visible = true;
            }
            else if (ddlRole.Text.Equals("Admin"))
            {
                lblqlmenu.Visible = false;
                cbxqlaccount.Visible = false;
                cbxqlgiaodien.Visible = false;
                cbxqlhoadon.Visible = false;
                cbxqlmenu.Visible = false;
                cbxqlthongtin.Visible = false;
                cbxqltour.Visible = false;
            }
        }
    }
}